let mixinList = require('../../common/mixinList');
Page({
    behaviors: [mixinList],
    data: {
        tab_active: 0,
        tabs: [{
            title: '待付款',
            button: [{
                name: '取消订单',
                mark: 'cancel'
            }, {
                name: '支付',
                mark: 'pay'
            }]
        }, {
            title: '备货中',
            button: [{ name: '查看订单', mark: '' }]
        }, {
            title: '待发货',
            button: [{ name: '查看订单', mark: '' }]
        }, {
            title: '待收货',
            button: [{
                name: '查看物流',
                mark: 'looklogistics'
            }, {
                name: '售后',
                mark: ''
            }, {
                name: '确认收货',
                mark: 'receiving'
            }]
        }, {
            title: '已完成',
            button: [{
                name: '查看订单',
                mark: ''
            }, {
                name: '评价',
                mark: ''
            }]
        }, {
            title: '已取消',
            button: [{ name: '查看订单', mark: '' }]
        }],
        reqUrl: '/api/order/index',
        usePrivateMethod: true,
    },
    onLoad: function(options) {
        const active = Number(options.active)
        this.setData({
            tab_active: active
        })
        active === 0 && this.getList(true)
    },
    getParamsData() {
        return {
            type: this.data.tab_active + 1,
            from: 'flower',
            limit: 4
        }
    },
    privateMethod(refresh, res) { //页面使用此Behavior私有方法,需要时在页面内覆盖此方法
        return new Promise(resolve => {
            res.data.forEach(order => {
                order.order.order_product.forEach(item => {
                    item.product_img = item.product_img + '!img_200'
                })
            })
            const { tab_active } = this.data
            if (tab_active === 0) {
                res.data.forEach(item => {
                    item.timeData = {}

                    let created_at = new Date(item.created_at)
                    let created_at_later = new Date(created_at.setSeconds(created_at.getSeconds() + 170))
                    const time_later = created_at_later.getTime()

                    let time = time_later - Date.now()
                    if (time < 0) time = 0

                    item.time = time
                })
            }
            resolve(res.data)
        })
    },
    operation({ mark }) {
        const { index, operation } = mark
        if (index == undefined) return
        if (operation === 'cancel') return this.cancel(index) //取消订单
        if (operation === 'pay') return this.pay(index) //立即支付
        if (operation === 'receiving') return this.receiving(index) //确认收货

        let url = `/pages/order_detail/index?order_id=${this.data.list[index].order.order_id}`
        if (operation === 'looklogistics') url += '&scrollIntoLogistics=true' //查看物流
        wx.navigateTo({
            url,
            events: { //详情页部分操作成功，此页裁剪一条
                splice: () => { this.splice(index) },
            }
        })
    },
    /**tabs切换事件 */
    onChange(event) {
        this.setData({
            tab_active: event.detail.index,
            list: []
        })
        this.getList(true)
    },
    /**倒计时改变事件 */
    onCountDownChange(e) {
        const { index, detail } = e.detail
        this.setData({
            ['list[' + index + '].timeData']: detail
        })
    },
    /**倒计时结束 */
    countFinish(e) {
        const { index } = e.detail
        this.splice(index)
    },
    /**点击取消订单 */
    cancel(index) {
        wx.$dialog.confirm({
            title: '提示',
            message: `您确定取消该订单吗？`,
            theme: 'round-button',
            beforeClose: (action) => new Promise((resolve) => {
                if (action === 'confirm') {
                    return this.request({
                        url: '/api/order/cancel',
                        method: 'POST',
                        data: {
                            order_id: this.data.list[index].order.order_id
                        }
                    }, () => {
                        resolve(true)
                        wx.showToast({
                            title: '取消成功',
                            icon: 'success'
                        })
                        this.splice(index)
                    }, () => {
                        resolve()
                    })
                }
                resolve(true)
            })
        })
    },
    /**点击立即支付 */
    pay(index) {
        this.request({
            url: '/api/payment/order',
            method: 'POST',
            loadingTip: '处理中',
            data: {
                code: 'wxpay',
                order_id: this.data.list[index].order.order_id
            }
        }, ({ data }) => {
            wx.requestPayment({
                timeStamp: data.pay_data.timeStamp,
                nonceStr: data.pay_data.nonceStr,
                package: data.pay_data.package,
                signType: data.pay_data.signType,
                paySign: data.pay_data.paySign,
                success: () => {
                    wx.showToast({
                        title: '支付成功',
                        icon: 'success'
                    })
                    this.splice(index)
                },
                fail: () => {
                    wx.showToast({
                        title: '支付失败',
                        icon: 'error'
                    })
                }
            })
        }, () => {
            wx.showToast({
                title: '支付失败',
                icon: 'error'
            })
        })
    },
    /**点击确认收货 */
    receiving(index) {
        wx.$dialog.confirm({
            title: '提示',
            message: `您确定收货吗？`,
            theme: 'round-button',
            beforeClose: (action) => new Promise((resolve) => {
                if (action === 'confirm') {
                    return this.request({
                        url: '/api/order/finished',
                        method: 'POST',
                        data: {
                            order_id: this.data.list[index].order.order_id
                        }
                    }, () => {
                        resolve(true)
                        wx.showToast({
                            title: '收货成功',
                            icon: 'success'
                        })
                        this.splice(index)
                    }, () => {
                        resolve()
                    })
                }
                resolve(true)
            })
        })
    },
    splice(index) {
        let data = this.data.list
        data.splice(index, 1)
        this.setData({
            list: data,
            empty: list.length === 0
        })
    }
})